package com.tt.plat8.core;

import com.tt.plat8.api.user.IUser;
import com.tt.plat8.api.user.IUserRole;
import com.tt.plat8.api.user.IUserSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.Request;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tangtao
 * Date: 14-2-19
 * Time: 上午11:30
 * 用户会话实体
 */
public class UserSession extends WebSession implements IUserSession {

    private IUser user;

    private Roles roles;

    public UserSession(Request request) {
        super(request);

    }

    @Override
    public IUser getUser() {

        return this.user;
    }

    @Override
    public void setUser(IUser user) {
        this.user=user;
        this.roles = new Roles();
        fillRoles(user);
    }

    private void fillRoles(IUser user){
         List<IUserRole> userRoles = user.getUserRoles();
        if(userRoles!=null){
              for(IUserRole userRole : userRoles){
                    roles.add(userRole.getRoleCode().getRoleCode());
              }
        }
    }

    public boolean hasAnyRole(Roles roles)
    {
        return this.roles.hasAnyRole(roles);
    }


    public boolean hasRole(String role)
    {
        return roles.hasRole(role);
    }
}
